using System;
using System.Linq;
using System.Numerics;
using UnityEngine;
using Object = UnityEngine.Object;
using Vector3 = UnityEngine.Vector3;

namespace ZFramework.Runtime
{
    public class Item : MonoBehaviour
    {
        private int _id;
        private string _assetPath;
        private ItemGroup _itemGroup;
        private ItemLogic _itemLogic;

        private Transform _initRoot;
        private Vector3 _initPosition;
        private Vector3 _initRotation;
        private Vector3 _initScale;

        public int Id
        {
            get { return _id; }
        }

        public string AssetPath
        {
            get { return _assetPath; }
        }

        public Object Obj
        {
            get { return gameObject; }
        }

        public ItemGroup ItemGroup
        {
            get { return _itemGroup; }
        }

        public ItemLogic Logic
        {
            get { return _itemLogic; }
        }

        public void OnInit(int itemId, string assetPath, ItemGroup itemGroup, bool isNewInstance, object userData)
        {
            _id = itemId;
            _assetPath = assetPath;
            if (isNewInstance)
            {
                _itemGroup = itemGroup;
            }
            else if (_itemGroup != itemGroup)
            {
                Log.Error("Item OnInit group不同");
                return;
            }

            _initRoot = transform.parent;
            _initPosition = transform.localPosition;
            _initRotation = transform.eulerAngles;
            _initScale = transform.localScale;

            ShowItemInfo showItemInfo = userData as ShowItemInfo;
            if (showItemInfo == null)
            {
                Log.Error("Item OnInit userData 类型不对");
                return;
            }

            Type itemLogicType = showItemInfo.ItemLogicType;
            if (itemLogicType == null)
            {
                return;
            }

            if (_itemLogic != null)
            {
                if (_itemLogic.GetType() == itemLogicType)
                {
                    _itemLogic.enabled = true;
                    return;
                }

                Destroy(_itemLogic);
                _itemLogic = null;
            }

            _itemLogic = gameObject.GetComponent(itemLogicType) as ItemLogic;
            if (_itemLogic == null)
                _itemLogic = gameObject.AddComponent(itemLogicType) as ItemLogic;

            if (_itemLogic == null)
            {
                Log.Error("添加itemlogic失败");
                return;
            }

            _itemLogic.OnInit(showItemInfo.UserData);

        }

        public void OnRecycle()
        {
            transform.SetParent(_initRoot, false);
            transform.localPosition = _initPosition;
            transform.eulerAngles = _initRotation;
            transform.localScale = _initScale;
            if (_itemLogic == null)
                return;

            _itemLogic.OnRecycle();
            _itemLogic.enabled = false;
            _id = 0;
        }

        public void OnShow(object userData)
        {
            ShowItemInfo showItemInfo = userData as ShowItemInfo;
            if (showItemInfo == null)
            {
                return;
            }

            if (_itemLogic == null)
                return;
            _itemLogic.OnShow(showItemInfo.UserData);
        }

        public void OnHide(object userData)
        {
            if (_itemLogic == null)
                return;
            _itemLogic.OnHide(userData);
        }

        public void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            if (_itemLogic == null)
                return;
            _itemLogic.OnUpdate(elapseSeconds, realElapseSeconds);
        }
    }
}